﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;

namespace VietKinsNutrition.ThuVien
{
    public partial class FrmTblDm_Loaimon : DevExpress.XtraEditors.XtraForm
    {
        ClsTblDm_LoaimonBLL ClsTblDm_Loaimonbll = new ClsTblDm_LoaimonBLL();
        int iBtn;

        public FrmTblDm_Loaimon()
        {
            InitializeComponent();
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (ClsTblDm_Loaimonbll.CheckNullTextbox(this.textEdit1) == false || ClsTblDm_Loaimonbll.FindSameName(1, this.textEdit1.Text) == false)
            {
                textEdit1.Focus();
                return;
            }
            if (iBtn == 1)
            {
                if (XtraMessageBox.Show("Bạn có muốn thêm mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
                this.ClsTblDm_Loaimonbll.Insert(this.textEdit1.Text);
            }
            else
            {
                if (XtraMessageBox.Show("Bạn có muốn sửa dữ liệu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
                this.ClsTblDm_Loaimonbll.Edit(Convert.ToInt32(gridView1.GetFocusedRowCellValue("Id")), this.textEdit1.Text);
            }
            this.ClsTblDm_Loaimonbll.ClearControl(this.textEdit1);
            ClsTblDm_Loaimonbll.BingSource_DataGrid(this.gridControl1);
            iBtn = 1;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            ClsTblDm_ThuvienmonanBLL ClsTblDm_Thuvienmonanbll = new ClsTblDm_ThuvienmonanBLL();

            if (ClsTblDm_Thuvienmonanbll.DsetSelectAllByLoaimonId(Convert.ToInt32(gridView1.GetFocusedRowCellValue("Id"))).Tables[0].Rows.Count > 0)
            {
                XtraMessageBox.Show("Đang có dữ liệu, hệ thống không cho phép xóa!", "Chú ý");
            }
            else
            {
                if (XtraMessageBox.Show("Bạn muốn chắc chắn xóa dữ liệu?", "Chú ý", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        this.ClsTblDm_Loaimonbll.Id = Convert.ToInt32(gridView1.GetFocusedRowCellValue("Id"));
                        this.ClsTblDm_Loaimonbll.Del();
                        ClsTblDm_Loaimonbll.BingSource_DataGrid(this.gridControl1);
                        XtraMessageBox.Show("Xóa dữ liệu thành công");
                    }
                    catch
                    {
                        XtraMessageBox.Show("Xóa thất bại");
                    }
                }
            }
            iBtn = 1;
            this.ClsTblDm_Loaimonbll.ClearControl(this.textEdit1);
        }

        private void FrmTblDm_Loaimon_Load(object sender, EventArgs e)
        {
            this.ClsTblDm_Loaimonbll.BingSource_DataGrid(this.gridControl1);
            iBtn = 1;
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            iBtn = 2;
            ClsTblDm_Loaimonbll.Id = Convert.ToInt32(gridView1.GetFocusedRowCellValue("id"));
            this.ClsTblDm_Loaimonbll.BingSource_InputControl(this.textEdit1, this.gridView1);
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            iBtn = 1;
            ClsTblDm_Loaimonbll.ClearControl(this.textEdit1);
        }
    }
}